package com.yoomiito.app.adapter.dialog

import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.formatNum
import com.yoomiito.app.model.comment.Comment
import com.yoomiito.app.saleNum
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.L
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.widget.SelectView
import de.hdodenhof.circleimageview.CircleImageView

class CommentAdapter(val context: Context, var datas: MutableList<Comment>) : androidx.recyclerview.widget.RecyclerView.Adapter<androidx.recyclerview.widget.RecyclerView.ViewHolder>() {

    var onItemClickListener: ((Int) -> Unit)? =null
    var onLikeClickListener:((Int)->Unit)?=null
    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
        return when (p1) {
            1 -> FirstItemViewHolder(LayoutInflater.from(context).inflate(R.layout.item_comment, p0, false))
            2 -> SecondItemViewHolder(LayoutInflater.from(context).inflate(R.layout.item_comment, p0, false))
            else -> ItemMoreViewHolder(LayoutInflater.from(context).inflate(R.layout.item_more_comment, p0, false))
        }
    }

    override fun getItemCount(): Int = datas?.size

    override fun onBindViewHolder(p0: androidx.recyclerview.widget.RecyclerView.ViewHolder, p1: Int) {
        val comment = datas?.get(p1)
        p0.itemView.setOnClickListener { onItemClickListener?.let { it1 -> it1(p1) } }
        when(comment?.type){
            1->{
                val holder = p0 as FirstItemViewHolder
                ImageLoader.getInstance().into(context,comment.headUrl,holder.userPic)
                holder.userName.text = "${comment.nickName} ${comment.time}"
                holder.userComment.text = comment.commentContent
                holder.likeSv.setSelect(comment.isLike==1)
                holder.likeSizeTv.text = comment.likeNumber.formatNum()
                holder.like.setOnClickListener { onLikeClickListener?.let { it(p1) } }
            }
            2->{
                val holder = p0 as SecondItemViewHolder
                ImageLoader.getInstance().into(context,comment.headUrl,holder.userPic)
                holder.userName.text = "${comment.nickName} ${comment.time}"
                if ("1" == comment.isReplay) {
                    holder.userComment.text =  comment.commentContent
                } else {
                    val s = "回复 ${comment.replayName}: ${comment.commentContent}"
                    val sp = SpannableString(s)
                    sp.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_999999)), 3, comment.replayName.length+3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                    holder.userComment.text = sp
                }

                holder.likeSv.setSelect(comment.isLike==1)
                holder.likeSizeTv.text = comment.likeNumber.formatNum()
                holder.like.setOnClickListener { onLikeClickListener?.let { it(p1) } }
            }
            3->{
                val holder = p0 as ItemMoreViewHolder
                holder.more.setOnClickListener {  }
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return datas?.get(position)!!.type
    }

    class FirstItemViewHolder(view: View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view){
        val userPic = itemView.findViewById<CircleImageView>(R.id.userPhoto)
        val userName = itemView.findViewById<TextView>(R.id.userName)
        val userComment = itemView.findViewById<TextView>(R.id.userComment)
        val like = itemView.findViewById<LinearLayout>(R.id.like)
        val likeSv = itemView.findViewById<SelectView>(R.id.likeSv)
        val likeSizeTv = itemView.findViewById<TextView>(R.id.likeSizeTv)

        init {
            val lp1 = itemView.layoutParams as androidx.recyclerview.widget.RecyclerView.LayoutParams
            lp1.leftMargin = DensityUtil.dp2px(12f)
        }
    }

    class SecondItemViewHolder(view: View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view){
        val userPic = itemView.findViewById<CircleImageView>(R.id.userPhoto)
        val userName = itemView.findViewById<TextView>(R.id.userName)
        val userComment = itemView.findViewById<TextView>(R.id.userComment)
        val like = itemView.findViewById<LinearLayout>(R.id.like)
        val likeSv = itemView.findViewById<SelectView>(R.id.likeSv)
        val likeSizeTv = itemView.findViewById<TextView>(R.id.likeSizeTv)
        init {
            val lp1 = itemView.layoutParams as androidx.recyclerview.widget.RecyclerView.LayoutParams
            lp1.leftMargin = DensityUtil.dp2px(56f)

            val lp = userPic.layoutParams as LinearLayout.LayoutParams
            val width = DensityUtil.dp2px(20f)
            lp.width = width
            lp.height = width

        }
    }

    class ItemMoreViewHolder(view: View): androidx.recyclerview.widget.RecyclerView.ViewHolder(view){
        val more = itemView.findViewById<LinearLayout>(R.id.moreComment)
        init {
            val lp1 = itemView.layoutParams as androidx.recyclerview.widget.RecyclerView.LayoutParams
            lp1.leftMargin = DensityUtil.dp2px(84f)
        }
    }

}